extends Node
#是否跳过抽排
var skip_draw_card: bool = false
#玩家牌组
var hand_pool: Array[Card] = []
#战斗时的手牌
var hand_cards: Array[Card] = []
#弃牌池
var discard_pool: CardPool = CardPool.new()
#抽牌池
var draw_pool: CardPool = CardPool.new()
#战斗时的总点数，可能通过卡牌临时增加点数
var hand_point
#可用点数，每回合开始时等于hand_point
var avail_point
#当前回合数
var current_round: int = 0
#每回合抽排数
var pre_draw_card: int = 2
var prop_dict = {}
var global_buff_damage: int:
	get = get_global_buff_damage
var global_buff_shield: int:
	get = get_global_buff_shield
#回合内数据
var round_data = RoundData.new()

var init_hand_pool_tres := preload("res://src/battle/card/card_info/init_hand_pool.tres")


func _ready():
	init_hand_pool_tres.load_cards()
	#新开游戏时，初始化牌组
	for i in init_hand_pool_tres.warrior_cards:
		hand_pool.append(i.duplicate())
	Events.card_pool_info_update.connect(_on_card_pool_info_update)
	round_data.clear_died_monster.connect(round_data._on_clear_died_monster)


func restart():
	hand_pool.clear()
	discard_pool = CardPool.new()
	draw_pool = CardPool.new()
	hand_cards.clear()


func start_battle():
	var player_state: PlayerInBattleState = Globals.player_state
	hand_point = player_state.point
	avail_point = hand_point
	draw_pool.card_list = hand_pool.duplicate(true)
	for i in range(0, min(draw_pool.card_list.size(), CardManager.pre_draw_card)):
		draw_card()


func end_battle():
	hand_cards = []
	discard_pool.end_battle()
	draw_pool.end_battle()
	hand_point = 0


func discard_poll_shuffle():
	draw_pool.card_list.append_array(discard_pool.card_list)
	discard_pool.card_list = []


func draw_card() -> Card:
	var card = draw_pool.draw_card()
	hand_cards.append(card)
	return card
	
	
func draw_card_no_shuffle() -> Card:
	var card = draw_pool.draw_card_no_shuffle()
	hand_cards.append(card)
	return card


func move_top_from_draw_pool(card_id):
	return draw_pool.move_top(card_id)


#为圣物或者全局加成考虑
func get_global_buff_damage():
	return global_buff_damage


#为圣物或者全局加成考虑
func get_global_buff_shield():
	return global_buff_shield


func _on_card_pool_info_update():
	for c in hand_cards:
		c.update_attribute()
	for c in discard_pool.card_list:
		c.update_attribute()
	for c in draw_pool.card_list:
		c.update_attribute()
		
#计算抽牌需要多少时间		
func pre_drad_card(card_number):
	var card_count = draw_pool.get_card_count() + discard_pool.get_card_count() 
	if card_count <= 0:
		return 0
	if draw_pool.get_card_count() < card_number:
		#洗牌+抽牌
		return 0.5 + card_number * 0.3
	else:
		return card_number * 0.3
